import {
  networkConfirmAcceptOrderApi,
  engineerConfirmAcceptOrderApi,
  getReplaceCustomerList,
  getOrderGoodsDetail,
  queryCodeForUpdateCode,
  updateWorkGoodsBindSnCode,
} from '@/api/order'
import { ref } from 'vue'

export const useNetworkConfirmAcceptOrder = () => {
  const acceptOrderLoading = ref(false)
  const acceptOrderParam = ref({
    id: '',
    networkId: '',
  })

  const acceptOrder = async () => {
    try {
      acceptOrderLoading.value = true
      const res = await networkConfirmAcceptOrderApi(acceptOrderParam.value)
      acceptOrderLoading.value = false
      return res.data
    } catch (error) {
      acceptOrderLoading.value = false
    }
  }

  return {
    networkAcceptOrderParam: acceptOrderParam,
    networkAcceptOrderLoading: acceptOrderLoading,
    networkAcceptOrder: acceptOrder,
  }
}

export const useEngineerConfirmAcceptOrder = () => {
  const acceptOrderLoading = ref(false)
  const acceptOrderParam = ref({
    id: '',
    networkId: '',
    engineerId: '',
  })

  const acceptOrder = async () => {
    try {
      acceptOrderLoading.value = true
      const res = await engineerConfirmAcceptOrderApi(acceptOrderParam.value)
      acceptOrderLoading.value = false
      return res.data
    } catch (error) {
      acceptOrderLoading.value = false
    }
  }

  return {
    engineerAcceptOrderParam: acceptOrderParam,
    engineerAcceptOrderLoading: acceptOrderLoading,
    engineerAcceptOrder: acceptOrder,
  }
}

export const useGetReplaceCustomers = () => {
  const getReplaceCustomersLoading = ref(false)
  const getReplaceCustomersKeyword = ref('')
  const replaceCustomers = ref<Array<OrderDetailAPIResponse.ReplaceCustomer>>([])

  const getReplaceCustomers = async () => {
    if (!getReplaceCustomersKeyword.value) {
      return
    }

    try {
      getReplaceCustomersLoading.value = true
      const res = await getReplaceCustomerList(getReplaceCustomersKeyword.value)
      getReplaceCustomersLoading.value = false
      if (res.data.code === 20000) {
        replaceCustomers.value = res.data.data.row
      }
      return res.data
    } catch (error) {
      getReplaceCustomersLoading.value = false
      return {
        data: {
          row: {},
        },
        code: 10000,
        message: '获取失败，请稍后重试',
        hasOtherCode: false,
        success: false,
        otherCodePrefix: null,
      }
    }
  }

  return {
    replaceCustomers,
    getReplaceCustomersKeyword,
    getReplaceCustomersLoading,
    getReplaceCustomers,
  }
}

/**
 * 获取工单详情的商品列表服务
 */
export const useGetOrderDetailGoodListService = () => {
  // 获取商品列表加载
  const getOrderDetailGoodListLoading = ref(false)

  // 商品列表
  const orderDetailGoodList = ref<Array<OrderDetailAPIResponse.GoodListItem>>([])

  // 获取商品列表参数
  const getOrderDetailGoodListParam = ref({
    limit: 1000,
    page: 1,
  })

  // 工单列表总条数
  const orderDetailGoodListTotal = ref(0)

  /**
   * 获取商品列表
   * @param orderId - 工单id
   */
  const getOrderDetailGoodList = async (orderId: string) => {
    try {
      getOrderDetailGoodListLoading.value = true
      const res = await getOrderGoodsDetail(orderId, getOrderDetailGoodListParam.value)
      getOrderDetailGoodListLoading.value = false
      if (res.data.code === 20000) {
        orderDetailGoodList.value = res.data.data.rows
        orderDetailGoodListTotal.value = Number(res.data.data.total)
      }
    } catch (error) {
      getOrderDetailGoodListLoading.value = false
    }
  }

  return {
    orderDetailGoodList,
    orderDetailGoodListTotal,
    getOrderDetailGoodListParam,
    getOrderDetailGoodListLoading,
    getOrderDetailGoodList,
  }
}

/**
 * 获取SN码信息服务（用于工单详情更新序列号）
 */
export const useGetSnCodeInfoForOrderDetailUpdateSnCodeService = () => {
  // 获取序列号信息加载
  const getSnCodeInfoLoading = ref(false)

  // 序列号信息
  const snCodeInfo = ref<OrderDetailAPIResponse.SnCodeInfoForOrderDetailUpdateSnCode>()

  /**
   * 获取SN码信息
   * @param snCode - SN码
   */
  const getSnCodeInfo = async (snCode: string) => {
    try {
      getSnCodeInfoLoading.value = true
      const res = await queryCodeForUpdateCode(snCode)
      getSnCodeInfoLoading.value = false
      if (res.data.code === 20000) {
        snCodeInfo.value = res.data.data
        return ''
      }

      return res.data.message
    } catch (error) {
      getSnCodeInfoLoading.value = false
      return '获取序列号信息失败，请稍后重试'
    }
  }

  return {
    snCodeInfo,
    getSnCodeInfoLoading,
    getSnCodeInfo,
  }
}

/**
 * 更改工单详情商品的序列号服务
 */
export const useOrderDetailGoodUpdateSnCodeService = () => {
  // 更新加载
  const updateLoading = ref(false)

  // 更新表单
  const updateForm = ref<OrderDetailAPiRequest.UpdateOrderDetailGoodSnCode>({
    workGoodsId: '',
    snCodes: [],
  })

  /**
   * 重置表单
   */
  const resetForm = () => {
    updateForm.value = {
      workGoodsId: '',
      snCodes: [],
    }
  }

  /**
   * 更新事件
   * @param orderId - 工单id
   */
  const update = async (orderId: string) => {
    try {
      updateLoading.value = true
      const res = await updateWorkGoodsBindSnCode(updateForm.value, orderId)
      updateLoading.value = false
      return res.data
    } catch (error) {
      updateLoading.value = false
    }
  }

  return {
    updateForm,
    updateLoading,
    update,
    resetForm,
  }
}
